package main

import (
	"context"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	pb "go-micro-demo/proto"
	"log"
)

func main() {
	// 新建一个服务实例
	service := micro.NewService(
		micro.Name("go.micro.yjw.course"),
		micro.Version("v1.0.0"),
		micro.Registry(etcd.NewRegistry(
			registry.Addrs("139.155.239.206:2379")),
		),
	)

	// 服务初始化
	service.Init()

	// 注册服务
	err := pb.RegisterCourseServiceHandler(service.Server(), new(CourseServiceImpl))
	if err != nil {
		log.Fatal(err)
	}

	// 运行服务
	service.Run()
}

// 实现Server API for CourseService service
type CourseServiceImpl struct {
}

func (s *CourseServiceImpl) ListForTop(ctx context.Context, req *pb.ListRequest, resp *pb.ListResponse) error {
	ret := make([]*pb.CourseModel, 0)
	ret = append(ret, NewCourseModel(101, "Python class"), NewCourseModel(102, "C++ class"))
	resp.Result = ret
	return nil
}

func NewCourseModel(id int32, name string) *pb.CourseModel {
	return &pb.CourseModel{
		CourseId:   id,
		CourseName: name,
	}
}

func NewCourseServiceImpl() *CourseServiceImpl {
	return &CourseServiceImpl{}
}
